import request from '@/utils/request';
import {download, toFormData} from '@/utils/common';

/**
 * 分页查询地区管理
 */
export async function pagePlaces(params) {
  const res = await request.get('/system/place/list', {params});
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 分页查询地区管理
 */
export async function listPlaces(params) {
  const res = await request.get('/system/place/list', {params});
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 生源所在地检索
 * @param params
 * @returns {Promise<any>}
 */
export async function listSearch(query) {
  const res = await request.get('/system/place/search/' + query);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}


/**
 * 添加地区管理
 */
export async function addPlace(data) {
  const res = await request.post('/system/place', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改地区管理
 */
export async function updatePlace(data) {
  const res = await request.put('/system/place', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除地区管理
 */
export async function removePlace(id) {
  const res = await request.delete('/system/place/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除地区管理
 */
export async function removePlaces(ids) {
  const res = await request.delete('/system/place/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出地区管理
 */
export async function exportPlaces(params) {
  const res = await request({
    url: '/system/place/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  download(res.data, `place_${Date.now()}.xlsx`);
}


/**
 * 下载地区导入模板
 * @returns {Promise<void>}
 */
export async function downloadTemplate() {
  const res = await request({
    url: '/system/place/downloadTemplate',
    method: 'POST',
    responseType: 'blob'
  });
  download(res.data, `place_info_template_${Date.now()}.xlsx`);
}


/**
 * 导入地区
 */
export async function importPlace(fileName) {
  let data = {};
  data.fileName = fileName;
  const res = await request.post('/system/place/importData', data);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

//获取导入数据进度
export async function importPlaceProgress(data) {
  const res = await request.get('/system/place/importDataProgress/' + data);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}


/**
 * 下载导入结果
 * @returns {Promise<void>}
 */
export async function downloadResult(params) {
  const res = await request({
    url: '/system/place/exportUploadResult',
    method: 'POST',
    responseType: 'blob',
    data: toFormData(params),
  });
  download(res.data, `place_import_result_${Date.now()}.xlsx`);
}


export async function fillFullName(id) {
  const res = await request.post('/system/place/fillFullName/' + id);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}


//根据层级查询地区列表
export async function selectByPlaceLevel(params) {
  const res = await request.get('/system/place/selectByPlaceLevel', {params});
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

